package edu.tms.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;

import edu.tms.bean.*;
import edu.tms.dao.AssessmentInfoDaoImp;
import edu.tms.dao.ClassInfoDaoImp;
import edu.tms.dao.ClassTeacherDaoImpl;
import edu.tms.dao.CourseAttendanceDaoImp;
import edu.tms.dao.CourseDaoImp;
import edu.tms.dao.HomeworkDaoImp;
import edu.tms.dao.SquadDaoImp;
import edu.tms.dao.StudentDaoImp;
import edu.tms.dao.TeacherDaoImp;
import edu.tms.dao.WorkQualityDaoImp;
import edu.tms.dao.WorkSubmitDaoImp;
import net.easymvc.annotation.Encoding;
import net.easymvc.annotation.FilePath;
import net.easymvc.annotation.FormData;
import net.easymvc.annotation.Param;
import net.easymvc.core.Action;


/**
 * 教师
 * @author Administrator
 *
 */
public class TeacherAction extends Action{
	/**
	 * 查看当前的最新评测信息
	 * 余浩洋
	 * @throws Exception
	 */
	@Encoding(value ="utf-8")
	public String classevaluatinginfo(@Param("ctions")String tiaojian){
		AssessmentInfoDaoImp asdao=new AssessmentInfoDaoImp();
		List<AssessmentInfo> ass = null;
		String list = null;
		try {
			ass=asdao.findassement(tiaojian);
			list = JSON.toJSONString(ass);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ajax:"+list;
	}
	
	int PageNo;
	String ClassName;
	/**
	 * 苟瑞波
	 * 查看学生信息通过 小组id ，班级Id,学生id，学生的姓名
	 * @return
	 */
	@Encoding("utf-8")
	public String findStuMsgByClassId(){
		StudentDaoImp sd=new StudentDaoImp();
		try {
			String stuId=getRequest().getParameter("stuId");
			String stuName=getRequest().getParameter("stuNamee");
			String pageNoo=getRequest().getParameter("pageNo");
			int pageNo=1;
			if(pageNoo!=null){
				pageNo=Integer.parseInt(pageNoo);
				PageNo=pageNo;
			}
			
			String teamId=getRequest().getParameter("teamId");
			String className=getRequest().getParameter("classNameteamId");
			
			int pages=(int)sd.pages(className,10);
			setRequest("pages", pages);
			//System.out.println(stuId+"ok"+stuName);
			List<Student> l = null;
			if(stuId!=null || stuName!=null){
				System.out.println("1");
				l= sd.findBustuidname(stuId,stuName, 1, 5);
			}else if(className!=null){
				System.out.println("2"+className);
				ClassName=className;		 
				l=sd.findByClassName(className,pageNo,10);
			}else if(teamId!=null){
				System.out.println("3");
				l= sd.findByClassNameTeamId(teamId,1,6);
			}
			setRequest("pageNo",pageNo);
			setRequest("stumsg",l);
			setRequest("className",className);
			return "/t/stuinfo.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	/**
	 * 苟瑞波
	 * 查看教师所属班级信息
	 * @return
	 */
	public String ClassInfo(){
		User user = (User) getSession("curUser");
		if(user == null)return "redirect:/login.html";
		ClassInfoDaoImp ci =new ClassInfoDaoImp();
		int tchId= user.getUserId();
		int pageNo=1;
		String pageNoo=getRequest().getParameter("pageNo");
		if(pageNoo!=null)pageNo=Integer.parseInt(pageNoo);
		try {
			
			int pages=(int)ci.pages();
			setRequest("pages",pages);
			List<ClassInfo> list=ci.findAll(tchId,pageNo, 10);
			setRequest("classinfo",list);
			System.out.println("总页数："+pages+"tchId:"+tchId);
			return "/t/classinfo.jsp";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
	}
	/**
	 * 苟瑞波
	 * 查找小组通过班级名称,组长ID，组长姓名
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "null" })
	@Encoding("utf-8")
	public String findTeamByTeam() throws Exception{
		SquadDaoImp sq=new SquadDaoImp();
		String squadName=getRequest().getParameter("squadName");//
		String stuName=getRequest().getParameter("stuName");
		String classname=getRequest().getParameter("className");
		List list=null;
		System.out.println(stuName);
		if(!classname.equals("gg")){list=sq.findAll(classname, 1,7);System.out.println("1");}
		else if(!stuName.equals("")){list=sq.findSquadByStuName(stuName);System.out.println("2");}
		else if(squadName!=null || squadName.length()>1){
			list=sq.findSquadBySquadName(squadName);
		}
		setRequest("teams",list);
		return "/t/teams.jsp";
	}
	

	/**
	 * 显示老师的个人信息
	 * @return
	 */
	public String tch(){
		//接受user
		User user=(User)getSession("curUser");
		if(user == null)return "redirect:/login.html";
		//加载老师dao
		TeacherDaoImp tea=new TeacherDaoImp();
		try {
			//接受dao返回的数组
			Teacher teacher =tea.findByKey(user.getUserId());
			if(teacher==null)setRequest("error_msg","信息出错请重试");
			//转发到网页中
			super.setRequest("tea", teacher);
			//返回到网页
			return "/t/information.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		//返回到网页
		return "/t/information.jsp";
	}
	
	/**
	 * 显示老师的课程信息
	 * @param account
	 * @return
	 */
	public String courseInfo(){
		//接受user
		User user=(User)getSession("curUser");
		if(user == null)return "redirect:/login.html";
		//dao
		CourseDaoImp dao = new CourseDaoImp();
		try {
			//接受dao返回的数组
			List<Course> list = dao.findByTchId(user.getUserId());
			if(list == null) setRequest("error_msg","信息出错请重试");
			//转发到网页中
			super.setRequest("courses", list);
			//返回到网页
			return "/t/teachingInfo.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		//返回到网页
		return "/t/teachingInfo.jsp";
	}
	
	/**
	 * 老师查看作业统计
	 * @param name
	 * @return
	 */
	@Encoding("UTF-8")
	public String countwork(@Param("pageNo") Integer pageNo,
			@Param("content") String content){
		User user = (User) getSession("curUser");
		if(user == null)return "redirect:/login.html";
		if(pageNo == null||pageNo <= 0) pageNo=1;//页码
		int pages=0;//总页数
		int tchId = user.getUserId();
		HomeworkDaoImp hw=new HomeworkDaoImp();
		try {
			if(content==null || content.length()==0){
				pages=(int) hw.selectTchIdPage(10,tchId);
			}else{
				pages=(int) hw.selectTchIdPage(tchId,content,10);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
		if(pageNo > pages && pages > 1)pageNo=pages;
		List<Homework> hws;
		WorkQualityDaoImp wqDao=new WorkQualityDaoImp();
		CourseDaoImp cDao=new CourseDaoImp();
		try {
			List<Course> cour=cDao.queryClassName(tchId);
			if(content==null || content.length()==0){
				hws=hw.findByTchId(tchId,pageNo,10);
				for (int i = 0; i < hws.size(); i++) {
					//统计合格率
					hws.get(i).setQualified((double) wqDao.qualified(hws.get(i).getClassName(),
							hw.countScore(hws.get(i).getHomeworkId())));
					//统计提交率
					hws.get(i).setSubmission((double) wqDao.submission(hws.get(i).getClassName(),
							hw.countWork(hws.get(i).getHomeworkId())));
				}
			}else{
				hws=hw.findByTchId(tchId,content, pageNo, 10);
				for (int i = 0; i < hws.size(); i++) {
					//统计合格率
					hws.get(i).setQualified((double) wqDao.qualified(hws.get(i).getClassName(),
							hw.countScore(hws.get(i).getHomeworkId())));
					//统计提交率
					hws.get(i).setSubmission((double) wqDao.submission(hws.get(i).getClassName(),
							hw.countWork(hws.get(i).getHomeworkId())));
				}
			}
			setRequest("pageNo", pageNo);
			setRequest("cour", cour);
			setRequest("pages", pages);
			setRequest("hws", hws);
			return "/t/countwork.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	/**
	 * 查看作业不合格人员
	 * @param homeworkId 作业编号
	 * @return ajax
	 */
	@Encoding("UTF-8")
	public String queryUnqualified(@Param("pageNo") Integer pageNo,
			@Param("homeworkId")Integer homeworkId,
			@Param("className")String className){
		if(pageNo == null||pageNo <= 0) pageNo=1;//页码
		HomeworkDaoImp hw=new HomeworkDaoImp();
		try {
			int pages=(int) hw.queryUnqualiPage(10,homeworkId);
			if(pageNo > pages && pages > 1)pageNo=pages;
			List<Homework> homes=hw.queryUnquali(homeworkId,pageNo,10);
			String hws=JSON.toJSONString(homes);
			String hom="{\"home\":"+hws+",\"pageNo\":\""+pageNo+"\","+"\"pages\":\""+pages+"\"}";
			return "ajax:"+hom;
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	/**
	 * 查看作业未提交人员
	 * @param homeworkId 作业编号
	 * @return ajax
	 */
	@Encoding("UTF-8")
	public String queryUnsubmission(@Param("pageNo") Integer pageNo,
			@Param("homeworkId")Integer homeworkId,
			@Param("className")String className){
		if(pageNo == null||pageNo <= 0) pageNo=1;//页码
		int pages=0;//总页数
		HomeworkDaoImp hw=new HomeworkDaoImp();
		try {
			pages = hw.queryUnsubmitPage(10,homeworkId,className).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
		if(pageNo > pages && pages > 1) pageNo = pages;
		try {
			List<Homework> homes=hw.queryUnsubmission(homeworkId,className,pageNo,10);
			
			String hws=JSON.toJSONString(homes);
			String hom="{\"home\":"+hws+","+"\"pageNo\":\""+pageNo+"\","+"\"pages\":\""+pages+"\"}";
			return "ajax:"+hom;
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	/**
	 * 显示以及查询批改作业页面数以及批改作业
	 * @param pageNo 页码
	 * @param content 学生姓名
	 * @param className 班级名称
	 * @param score 分数
	 * @param qualistyId 评分Id
	 * @return
	 */
	@Encoding("UTF-8")
	public String correting(@Param(value="pageNo")Integer pageNo,
			@Param(value="stuName")String content,
			@Param(value="className")String className,
			@Param(value="score")String score,
			@Param(value="submitId")Integer submitId){
		User user=(User) getSession("curUser");
		if(user==null)return "redirect:/login.html";
		if(pageNo==null || pageNo<=0)pageNo=1;
		int pages=0;//总页数
		int tchId = user.getUserId();
		WorkSubmitDaoImp wDao=new WorkSubmitDaoImp();
		try {
			//获取数据总页数
			if(content==null || content.length()==0){
				pages=(int) wDao.selectPages(10,tchId);
			}else{
				pages=(int) wDao.selectPages(10, tchId,content,className);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//判断页数与总页数大小
		if(pageNo > pages && pages > 1) pageNo = pages;
		WorkQualityDaoImp wqDao=new WorkQualityDaoImp();
		CourseDaoImp cDao=new CourseDaoImp();
		try {
			List<Course> cour=cDao.queryClassName(tchId);
			//score判断是否输入内容
			if(score==null || score.length()==0){}
			else{//若score有数据则进行分数修改
				if(score.equals("A")||score.equals("B")||score.equals("C")||
						score.equals("D")||score.equals("E")){
					WorkQuality work=new WorkQuality();
					//向work实体类中添加数据
					work.setWorkSubmitId(submitId);
					work.setCreateTime(new Date());
					work.setWorkLevel(score);
					work.setWorkState(1);
					//修改分数
					wqDao.save(work);
				}else{
					System.out.println("请输入A,B,C,D,E进行评分");
				}
			}
			List<WorkSubmit> works;
			//查询学生提交作业数据
				if(className==null || className.length()==0){
					works=wDao.findAll(tchId, pageNo, 10);
				}else{
					if(content==null || content.length()==0){
						works=wDao.findAll(tchId, className,pageNo,10);
					}else{
						works=wDao.findAll(tchId,content,className,pageNo, 10);
					}
				}
			//转发数据
			setRequest("works", works);
			setRequest("cour", cour);
			setRequest("pageNo", pageNo);
			setRequest("pages", pages);
			return "/t/correting.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	
	/**
	 * 老师查看已布置作业
	 * @param pageNo 页数
	 * @param className 班级名称
	 * @return
	 */
	@Encoding("UTF-8")
	public String releaseWork(@Param(value="pageNo")Integer pageNo,
			@Param(value="className")String className){
		User user=(User) getSession("curUser");
		if(user==null)return "redirect:/login.html";
		if(pageNo==null || pageNo<=0)pageNo=1;
		int pages=0;//总页数
		int tchId=user.getUserId();
		HomeworkDaoImp hw=new HomeworkDaoImp();
		try {
			//获取数据总页数
			if(className==null || className.length()==0){
				pages=(int) hw.selectTchIdPage(6,tchId);
			}else{
				pages=(int) hw.selectTchIdPage(tchId,className,6);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		//判断页数与总页数大小
		if(pageNo > pages && pages > 1) pageNo = pages;
		CourseDaoImp cDao=new CourseDaoImp();
		try {
			List<Course> cour=cDao.queryClassName(tchId);
			List<Homework> hws;
			if(className==null || className.length()==0){
				hws=hw.findByTchId(tchId, pageNo, 6);
				//统计已提交作业人数和作业合格人数
				for (int i = 0; i < hws.size(); i++) {
					double countWork=hw.countWork(hws.get(i).getHomeworkId());
					double countScore=hw.countScore(hws.get(i).getHomeworkId());
					hws.get(i).setCountWork(countWork);
					hws.get(i).setCountScore(countScore);
				}
			}else{
				hws=hw.findByTchId(tchId, className, pageNo, 6);
				//统计已提交作业人数和作业合格人数
				for (int i = 0; i < hws.size(); i++) {
					double countWork=hw.countWork(hws.get(i).getHomeworkId());
					double countScore=hw.countScore(hws.get(i).getHomeworkId());
					hws.get(i).setCountWork(countWork);
					hws.get(i).setCountScore(countScore);
				}
			}
			setRequest("hws", hws);
			setRequest("cour", cour);
			setRequest("pageNo", pageNo);
			setRequest("pages", pages);
			return "/t/assignment.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	
	/**
	 * 老师布置作业
	 * @param homework 数据封装
	 * @return
	 */
	@FormData
	@Encoding("UTF-8")
	public String assignment(Homework homework,
			@FilePath String filePath){
		User user=(User) getSession("curUser");
		if(user==null)return "redirect:/login.html";
		int tchId = user.getUserId();
		HomeworkDaoImp hw=new HomeworkDaoImp();
		CourseDaoImp cDao=new CourseDaoImp();
		try {
			//查询课程名称以及专业名称
			Course cour=cDao.queryName(homework.getClassName(), tchId);
			homework.setCourseName(cour.getCourseName());
			homework.setMajorName(cour.getMajorName());
			homework.setCreateTime(new Date());
			homework.setHomeworkFile(filePath);
			homework.setSubmitState(1);
			homework.setTchId(tchId);
			hw.save(homework);
			return releaseWork(1, "");
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	/**
	 * 删除作业信息
	 * @param homeworkId
	 * @return
	 */
	@Encoding("UTF-8")
	public String delete(@Param(value="homeworkId")Integer homeworkId){
		User user=(User) getSession("curUser");
		if(user==null)return "redirect:/login.html";
		HomeworkDaoImp hw=new HomeworkDaoImp();
		try {
			hw.delete(homeworkId);
			return releaseWork(1, "");
		} catch (Exception e) {
			e.printStackTrace();
			return "/error.jsp";
		}
	}
	
//	/**
//	 * 获取教师所教授的班级，ajax传递
//	 * @return
//	 */
//	@Encoding("utf-8")
//	public String getTchClass(){
//		User user = (User) getSession("curUser");
//		if(user == null) return "redirect:/login.html";
//		ClassTeacherDaoImpl classTeacherDao = new ClassTeacherDaoImpl();
//		//查出该教师所教授的班级
//		try {
//			List<ClassTeacher> classList = classTeacherDao.findAll(user.getUserId());
//			String classes = JSON.toJSONString(classList);
//			return "ajax:"+classes;
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return "ajax:null";
//	}
	
	/**
	 * 获取教师所教授的课程 ajax传递
	 * @return
	 */
	@Encoding("utf-8")
	public String getCourses(@Param("classId")Integer classId){
		User user = (User) getSession("curUser");
		if(user == null) return "ajax:forward:/login.html";
		if(classId == null) return "ajax:信息获取失败";
		//获取教师所教授的课程
		CourseDaoImp dao = new CourseDaoImp();
		try {
			List<Course> courses = dao.findAll(user.getUserId(),classId);
			String json = JSON.toJSONString(courses);
			return "ajax:"+json;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ajax:";
	}
	
	/**
	 * 教师进入课程考勤页面
	 * @return
	 */
	@Encoding("utf-8")
	public String  attendance(){
		User user = (User) getSession("curUser");
		if(user == null) return "redirect:/login.html";
		ClassTeacherDaoImpl classTeacherDao = new ClassTeacherDaoImpl();
//		CourseAttendanceDaoImp courseDao = new CourseAttendanceDaoImp();
		try {
			//查出该教师所教授的班级
			List<ClassTeacher> classList = classTeacherDao.findAll(user.getUserId());
			//查询该教师本学期教授的课程考勤
//			List<CourseAttendance> aList = courseDao.findAll(user.getUserId(), getTerm());
			
			
			setRequest("classes", classList);
			return "/t/attendance.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 获取当前学期
	 * @return
	 */
	public String getTerm(){
		Calendar cal = Calendar.getInstance();  
		int year = cal.get(Calendar.YEAR);
		String y = String.valueOf(year);
		y = y.substring(2) + "年";
		int m = cal.get(Calendar.MONTH) + 1;
		if(m > 2 && m < 8) y = y + "春季";
		else if(m > 8 && m < 12)  y = y + "秋季";
		return y;
	}
	
	//教师课堂打卡
	@Encoding("utf-8")
	public String courseadd(CourseAttendance course){
		//TODO null
		course.setCreateTime(new Date());
		CourseAttendanceDaoImp caddao =new CourseAttendanceDaoImp();
		try {
			caddao.save(course);
			return "teacher/attendance.do";
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	//查询历史课程考勤
	@Encoding("utf-8")
	public String selectcourse(
			@Param("pageNo")Integer pageNo,
			@Param("week")String week,
			@Param("ClassId")Integer classid){
		if(pageNo == null || pageNo < 1) pageNo = 1;
		CourseAttendanceDaoImp caddao =new CourseAttendanceDaoImp();
		try {
			List<CourseAttendance> courselist =caddao.findAll(week,classid,pageNo,10);
			int pages=(int)caddao.findAll(week,classid);
			setRequest("coursel", courselist);
			setRequest("pages", pages);
			setRequest("pageNo", pageNo);
			return "teacher/maincourse.do";
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
